#include "qsoftundostack.h"

#include "qpropertyundocommand.h"
#include "qcommandobject.h"
#include "../qsoftcore.h"

#include <QAction>

using namespace CORELIB;

QSoftUndoStack::QSoftUndoStack(QObject *parent) :
    QUndoStack(parent)
{
}

void QSoftUndoStack::redo_slot()
{
    QPropertyUndoCommand* cmd=(QPropertyUndoCommand*)this->command(this->index());

    if(cmd!=NULL)
    {
        if(QSoftCore::getCore()->emit_pageAt(cmd->object()->property("page_position")))
        {
            return;
        }
    }
    QUndoStack::redo();
}

void QSoftUndoStack::undo_slot()
{
    QPropertyUndoCommand* cmd=(QPropertyUndoCommand*)this->command(this->index()-1);

    if(cmd!=NULL)
    {
        if(QSoftCore::getCore()->emit_pageAt(cmd->object()->property("page_position")))
        {
            return;
        }
    }
    QUndoStack::undo();
}


QAction* QSoftUndoStack::createRedoAction(QObject *parent, const QString &prefix) const
{
    QAction* ac=QUndoStack::createRedoAction(parent,prefix);
    disconnect(ac,SIGNAL(triggered()),this,SLOT(redo()));
    connect(ac,SIGNAL(triggered()),this,SLOT(redo_slot()));
    return ac;
}

QAction* QSoftUndoStack::createUndoAction(QObject *parent, const QString &prefix) const
{
    QAction* ac=QUndoStack::createUndoAction(parent,prefix);
    disconnect(ac,SIGNAL(triggered()),this,SLOT(undo()));
    connect(ac,SIGNAL(triggered()),this,SLOT(undo_slot()));
    return ac;
}
